package com.imoresoft.magic.parser.templet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ambitor.grass.sql.dao.SuperDao;
import com.ambitor.grass.util.data.DataMap;
import com.ambitor.grass.util.data.DatasetList;
import com.ambitor.grass.util.data.IData;
import com.ambitor.grass.util.data.IDataset;

public abstract class IUserTempletParser {

	private SuperDao superDao;

	protected IDataset itemList;

	protected String tilte;

	protected int numPerTr;

	protected IData userTempletStyle = new DataMap();

	protected IData buyerData = new DataMap();

	protected IData templetSkin = new DataMap();

	protected IData templetCode = new DataMap();

	protected final Logger logger = LoggerFactory.getLogger(this.getClass());

	public IUserTempletParser(SuperDao superDao, IData userTempletStyle,
			IDataset itemList, IData buyerData, String title, int numPerTr)
			throws Exception {
		this.tilte = title;
		this.superDao = superDao;
		this.itemList = itemList;
		this.numPerTr = numPerTr;
		this.buyerData =buyerData;
		this.userTempletStyle = userTempletStyle;

		int templetId = userTempletStyle.getInt("TEMPLET_ID");
		int skinId = userTempletStyle.getInt("SKIN_ID");
		initTempletCode(templetId);
		intiSkin(templetId, skinId);
	}

	@SuppressWarnings("unchecked")
	private void intiSkin(int templetId, int skinId) throws Exception {
		IData param = new DataMap();
		param.put("TEMPLET_ID", templetId);
		param.put("SKIN_ID", skinId);
		IDataset dataset = new DatasetList();
		try {
			dataset = superDao.queryList("TD_M_TEMPLET_SKIN", "attr,value",
					param);
		} catch (Exception e) {
			logger.warn("", e);
		}

		for (int i = 0; i < dataset.count(); i++) {
			IData data = dataset.getData(i);
			templetSkin.put(data.getString("ATTR").toUpperCase(), data
					.getString("VALUE"));
		}
	}

	@SuppressWarnings("unchecked")
	private void initTempletCode(int templetId) throws Exception {
		IData param = new DataMap();
		param.put("TEMPLET_ID", templetId);
		IDataset dataset = new DatasetList();
		try {
			dataset = superDao.queryList("TD_M_TEMPLET_CODE",
					"templet_code_id,templet_code", param);
		} catch (Exception e) {
			logger.warn("查询TD_M_TEMLET出错", e);
		}

		for (int i = 0; i < dataset.count(); i++) {
			IData data = dataset.getData(i);
			templetCode.put(data.getString("TEMPLET_CODE_ID").toUpperCase(),
					data.getString("TEMPLET_CODE"));
		}
	}

	public abstract String buildDisplayHtml() throws Exception;
}
